﻿using System;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using System.Threading;

namespace AE_Installs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Init();
        }
        
        private void Init()
        {
            this.textBox1.Parent = this.panel1;
            this.pictureBox1.Parent = this.panel1;

            this.panel1.Controls["pictureBox1"].Show();
            this.panel1.Controls["textBox1"].Hide();
            this.treeView1.NodeMouseClick += TreeView1_NodeMouseClick;


            string[] curr = System.IO.Directory.GetFiles("./AE插件包");
            foreach (string s in curr)
            {
                ZipManager.UnZipXml(s, "", "", null);
                ZipManager.InitUnZipFile();
                InitAexTreeNode();
            }

        }

        private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            SetParentCheckedState(e.Node, e.Node.Checked);
            SetChildCheckedState(e.Node, e.Node.Checked);
            CheckAll(e.Node,e.Node.Checked);
        }

        #region 初始化TreeView

        public void InitTreeView()
        {

        }
        #endregion
        /// <summary>
        /// 提交注册的时候 调用
        /// </summary>
        /// <param name="node"></param>
        private void UnZip(TreeNode node)
        {
            foreach(TreeNode tn in this.treeView1.Nodes)
            {
                GetNodeCheckedText(tn);
            }
        }

        private void GetNodeCheckedText(TreeNode node)
        {
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode n in node.Nodes)
                {
                    GetNodeCheckedText(n);
                }
            }
            else
            {
                if (node.Checked)
                    list.Add(node.Text);
            }
        }

        private string AddList(string aexname)
        {
            string[] aexpathnames = System.IO.Directory.GetFiles("./AE插件包");
            foreach(string s in aexpathnames)
            {
                if(s.Contains(aexname))
                {
                    return s;
                }
            }
            return null;
        }

        private void  CheckAll(TreeNode currentNode,bool state)
        {
            bool flag = true;
            currentNode.Checked = state;
            if(currentNode.Parent != null)
            {
                foreach(TreeNode n in currentNode.Parent.Nodes)
                {
                    if(n.Checked == false)
                    {
                        flag = false;
                        break;
                    }
                }
                currentNode.Parent.Checked = flag;
            }
        }

        private void SetParentCheckedState(TreeNode currentNode,bool state)
        {
            currentNode.Checked = state;
            if (currentNode.Parent != null)
            {
                currentNode.Parent.Checked = false;

                SetParentCheckedState(currentNode.Parent,false);
            }
        }

        private void SetChildCheckedState(TreeNode currentNode,bool state)
        {
            currentNode.Checked = state;
            if (currentNode.GetNodeCount(true) > 0)
            {
                foreach(TreeNode n in currentNode.Nodes)
                {
                    n.Checked = state;
                    SetChildCheckedState(n,state);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //"ConfigDir" = "C:\\Program Files\\BorisFX\\ContinuumAE"
            //"RlmLicensePath" = "C:\\\\ProgramData\\\\GenArts\\\\rlm"
            //"PFDirectory" = "C:\\\\Program Files\\\\BorisFX\\\\ContinuumAE\\\\11"
            //"PDDirectory" = "C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11"
            //"Custom Textures" = "C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11\\Presets\\\\Custom Textures"
            //"Resources" = "C:\\\\Program Files\\\\BorisFX\\\\ContinuumAE\\\\11\\\\lib\\\\Resources"
            //"Presets" = "C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11\\\\Presets"
            //"TSPlugins" = "C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11\\\\TSPlugins"

            ZipManager.WriteRegister("",
                "{\"ConfigDir\":\"C:\\\\Program Files\\\\BorisFX\\\\ContinuumAE\"," +
                 "\"RlmLicensePath\":\"C:\\\\ProgramData\\\\GenArts\\\\rlm\"," +
                 "\"PFDirectory\":\"C:\\\\Program Files\\\\BorisFX\\\\ContinuumAE\\\\11\"," +
                 "\"PDDirectory\":\"C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11\"," +
                 "\"Custom Textures\":\"C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11\\\\Presets\\\\Custom Textures\"," +
                 "\"Resources\":\"C:\\\\Program Files\\\\BorisFX\\\\ContinuumAE\\\\11\\\\lib\\\\Resources\"," +
                 "\"Presets\":\"C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11\\\\Presets\"," +
                 "\"TSPlugins\":\"C:\\\\ProgramData\\\\BorisFX\\\\ContinuumAE\\\\11\\\\TSPlugins\"," +
                 "}"
                );

            ZipManager.StreamTest();
        }

        private void Decompress()
        {
            //Thread thread = new Thread(() =>
            //{
            //    ZipManager.UnZip(@"./(名称汉化)BCC插件Boris Continuum Complete 11  409.5 MB.zip",
            //                            "","BorisFX","", UpdateDecompressProgess);
            //});
            //thread.Start();

            //Thread thread = new Thread(() =>
            //{
            //    ZipManager.UnZipXml(@"./Test.zip", "./",
            //    "", UpdateDecompressProgess);
            //});
            //thread.Start();

            //ZipManager.InitUnZipFile();
            //InitAexTreeNode();


            //ZipManager.UnZipXml("./(名称汉化)BCC插件Boris Continuum Complete 11  409.5 MB.zip", "", "", UpdateDecompressProgess);
            UnZip(this.treeView1.Nodes[0]);
            //list.Add("(名称汉化)BCC插件Boris Continuum Complete 11  409.5 MB");
            Thread thread = new Thread(Go);
            thread.Start();
        }

        private void Go(object o)
        {
            foreach(string str in list)
            {
                AEX aex = ZipManager.GetAex(str);
                if(aex != null)
                {
                    ZipManager.Go("./AE插件包/"+aex.Name+".zip" , "", "", aex.Regname,str, UpdateDecompressProgess);
                }
            }
            //有注册表就写注册表
            foreach(TreeNode n in this.treeView1.Nodes)
            {
                if(n.Checked)
                {
                    AEX aex = ZipManager.GetAex(n.Text);
                    if(aex != null)
                    {
                        ZipManager.WriteRegister("", jsonString(aex));
                    }
                }
            }
        }

        private string jsonString(AEX aex)
        {
            string result = "";
            foreach(string key in aex.RegisterDic.Keys)
            {
                result += key + ":" + aex.RegisterDic[key] +",";
            }
            result = "{" + result.Substring(0, result.Length - 1) + "}";
            return result;
        }

        private void InitAexTreeNode()
        {
            System.Collections.Generic.Dictionary<string,AEX> Dic = ZipManager.AEXDic;
            foreach (string key in Dic.Keys)
            {
                TreeNode tree = new TreeNode(Dic[key].Name);
                TreeNode[] children = new TreeNode[Dic[key].ChildrenDirectorys.Length];
                
                for(int i=0;i<children.Length;++i)
                {
                    children[i] = new TreeNode(Dic[key].ChildrenDirectorys[i]);
                    if(Dic[key].EN != "" || Dic[key].CH != "")
                    {

                    }
                }
                tree.Nodes.AddRange(children);
                this.treeView1.Nodes.AddRange(new TreeNode[] { tree });
            }
        }

        System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();

        private void LoadChecked()
        {
            
        }

        private TreeNodeCollection GetChild(TreeNode currnode)
        {
            if(currnode.Nodes!=null)
            {
                if(currnode.Nodes.Count > 0)
                {
                    return currnode.Nodes;
                }
            }

            return null;
        }

        private void UpdateDecompressProgess(float now,float max)
        {
            Console.WriteLine($"已经解压[{(now / max) * 100}%]");
            UpdateLabel($"已经解压[{(now / max)*100}%]");
        }

        private void UpdateLabel(string str)
        {
            Action act = () => { this.label6.Text = str; };
            this.Invoke(act);
        }

        private void Compress()
        {
            FastZip zip = new FastZip();
            zip.CreateZip("./FastZip.zip", "./W-Cool", true, "");
            //using (ZipFile zip = ZipFile.Create("./W-Cool-Zip.zip"))
            //{
            //    zip.BeginUpdate();
            //    zip.AddDirectory("W-Cool");
            //    zip.CommitUpdate();
            //}
        }
        

        private void button2_Click(object sender, EventArgs e)
        {
            Decompress();
        }

        private bool Flag = false;
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowWelcome();
        }

        private void ShowWelcome()
        {
            Flag = !Flag;
            if (Flag)
            {
                this.pictureBox1.Hide();
                this.textBox1.Show();
            }
            else
            {
                this.pictureBox1.Show();
                this.textBox1.Hide();
            }
        }

        
    }
}
